package com.example.treehole.ui.page.postDetail

import androidx.activity.compose.BackHandler
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.example.treehole.LocalPostDetailViewModel
import com.example.treehole.LocalUserViewModel
import com.example.treehole.ui.component.RefreshBox
import com.example.treehole.ui.theme.backCyan
import kotlinx.coroutines.launch

val LocalCreateCommentViewModel =
    compositionLocalOf<CreateCommentViewModel> { error("CreateCommentViewModel not provided") }

@Composable
fun PostDetailScreen() {
    val userViewModel = LocalUserViewModel.current
    val userId by userViewModel.userId.collectAsState()
    val jwlToken by userViewModel.jwlToken.collectAsState()
    val scope = rememberCoroutineScope()

    val postDetailViewModel = LocalPostDetailViewModel.current
    val postDetail by postDetailViewModel.postDetail.collectAsState()

    val localCreateCommentViewModel: CreateCommentViewModel = viewModel()
    val isCommenting = localCreateCommentViewModel.isCommenting

    val focusRequester = remember { FocusRequester() }

    // 下拉刷新
    val isRefreshing = remember { mutableStateOf(false) }

    CompositionLocalProvider(LocalCreateCommentViewModel provides localCreateCommentViewModel) {
        Scaffold(
            topBar = { Box(modifier = Modifier.height(0.dp)) },
            bottomBar = {
                if (!isCommenting.value) {
                    postDetail?.let { BottomCommentBar(it) }
                        ?: Box(modifier = Modifier.height(0.dp))
                }
            }
        ) { padding ->
            RefreshBox(
                isRefreshing = isRefreshing,
                onRefresh = {
                    scope.launch {
                        userId?.let {
                            postDetail?.let { it1 ->
                                postDetailViewModel.getPostDetail(jwlToken, it1.postId, it)
                            } ?: postDetailViewModel.getPostDetailById(jwlToken, it)
                        }
                    }
                },
                modifier = Modifier
                    .fillMaxSize()
                    .padding(padding),
            ) {
                postDetail?.let { ContentCard(it) }
                    ?: Box(
                        modifier = Modifier
                            .fillMaxSize()
                            .background(backCyan),
                        contentAlignment = Alignment.Center
                    ) {
                        Text(
                            text = if (!isRefreshing.value) "好像出了一点点小错误..." else "正在拼命加载中",
                            fontSize = 24.sp,
                            color = Color.White
                        )
                    }
            }
            if (isCommenting.value) {
                Box(
                    modifier =
                    Modifier
                        .fillMaxSize()
                        .background(Color.Black.copy(alpha = 0.2f))
                        .clickable {
                            localCreateCommentViewModel.updateCommentingState(false)
                        }
                ) {
                    Box(modifier = Modifier.align(Alignment.BottomCenter)) {
                        postDetail?.let { InputCommentCard(focusRequester, it.postId) }
                    }
                }
                BackHandler { localCreateCommentViewModel.updateCommentingState(false) }
            }
        }
    }

    // 自动打开键盘
    LaunchedEffect(isCommenting.value) {
        if (isCommenting.value) {
            focusRequester.requestFocus()
        }
    }
}
